#include "mainwindow.h"

int MainWindow::offM(int j)
{
    int offM[3];
    offM[j]=0;
    for (int i=4*j;i<4*(j+1);i++)
        if(boxm1[i]->checkState()==Qt::Checked) offM[j]++;
    return offM[j];
}

void MainWindow::changeLabelValue(int i,QLabel **label,int signVal)
{
    if(signVal==0)
    {
        label[i]->setNum(0);
        return;
    }
    if(signVal==1)
    {
        label[i]->setNum(1);
        return;
    }
    else
    {
        label[i]->setText("");
        return;
    }
}

void MainWindow::changeMLineColorAfterInput(){
    M1=0;
    M2=0;
    M3=0;
    for(int i=0;i<12;i++)
    {
        if(boxm1[i]->checkState()==Qt::Checked){
            switch ((int)i/4)
            {
                case 0:
                {
                    if(signalValue[i%4]!=-1)
                    {
                        if(signalValue[i%4]>0)
                        {
                            fm1[i%4]->setStyleSheet(colorTrue1);
                            fme1[i%4]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm1[i%4]->setStyleSheet(colorTrue0);
                            fme1[i%4]->setStyleSheet(colorTrue0);
                        }
                        M1+=signalValue[i%4];
                        changeMLineColor((int)i/4);
                    }
                    else
                    {
                        fm1[i%4]->setStyleSheet(colorDefault);
                        fme1[i%4]->setStyleSheet(colorDefault);
                    }
                    break;
                }
                case 1:
                {
                    if(signalValue[i%4]!=-1)
                    {
                        if(signalValue[i%4]>0)
                        {
                            fm2[i%4]->setStyleSheet(colorTrue1);
                            fme2[i%4]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm2[i%4]->setStyleSheet(colorTrue0);
                            fme2[i%4]->setStyleSheet(colorTrue0);
                        }
                        M2+=signalValue[i%4];
                        changeMLineColor((int)i/4);
                    }
                    else
                    {
                        fm2[i%4]->setStyleSheet(colorDefault);
                        fme2[i%4]->setStyleSheet(colorDefault);
                    }
                    break;
                }
                case 2:
                {
                    if(signalValue[i%4]!=-1)
                    {
                        if(signalValue[i%4]>0)
                        {
                            fm3[i%4]->setStyleSheet(colorTrue1);
                            fme3[i%4]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm3[i%4]->setStyleSheet(colorTrue0);
                            fme3[i%4]->setStyleSheet(colorTrue0);
                        }
                        M3+=signalValue[i%4];
                        changeMLineColor((int)i/4);
                    }
                    else
                    {
                        fm3[i%4]->setStyleSheet(colorDefault);
                        fme3[i%4]->setStyleSheet(colorDefault);
                    }
                    break;
                }
            }
        }
    }
}

void MainWindow::changeMLineColor(int i)
{
    switch (i)
    {
        case 0:
        {
            signalValue[4]=M1%2;
            if(Error==5) signalValue2[4]=!(M1%2);
            else signalValue2[4]=M1%2;
            if(signalValue[4])
            {
                f_A[0]->setStyleSheet(colorTrue1);
                fHConl[0]->setStyleSheet(colorTrue1);
                fVConl[0]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM(i))
                {
                    f_A[0]->setStyleSheet(colorTrue0);
                    fHConl[0]->setStyleSheet(colorTrue0);
                    fVConl[0]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_A[0]->setStyleSheet(colorDefault);
                    fHConl[0]->setStyleSheet(colorDefault);
                    fVConl[0]->setStyleSheet(colorDefault);
                    signalValue[4]=-1;
                    signalValue2[4]=-1;
                }
            }
            break;
        }
        case 1:
        {            
            signalValue[5]=M2%2;
            if(Error==6) signalValue2[5]=!(M2%2);
            else signalValue2[5]=M2%2;
            if(signalValue[5])
            {
                f_A[1]->setStyleSheet(colorTrue1);
                fHConl[1]->setStyleSheet(colorTrue1);
                fVConl[1]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM(i))
                {
                    f_A[1]->setStyleSheet(colorTrue0);
                    fHConl[1]->setStyleSheet(colorTrue0);
                    fVConl[1]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_A[1]->setStyleSheet(colorDefault);
                    fHConl[1]->setStyleSheet(colorDefault);
                    fVConl[1]->setStyleSheet(colorDefault);
                    signalValue[5]=-1;
                    signalValue2[5]=-1;
                }
            }
            break;
        }
        case 2:
        {
            signalValue[6]=M3%2;
            if(Error==7) signalValue2[6]=!(M3%2);
            else signalValue2[6]=M3%2;
            if(signalValue[6])
            {
                f_A[2]->setStyleSheet(colorTrue1);
                fHConl[2]->setStyleSheet(colorTrue1);
                fVConl[2]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM(i))
                {
                    f_A[2]->setStyleSheet(colorTrue0);
                    fHConl[2]->setStyleSheet(colorTrue0);
                    fVConl[2]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_A[2]->setStyleSheet(colorDefault);
                    fHConl[2]->setStyleSheet(colorDefault);
                    fVConl[2]->setStyleSheet(colorDefault);
                    signalValue[6]=-1;
                    signalValue2[6]=-1;
                }
            }
            break;
        }
    }
    changeLabelValue(i,l_A,signalValue[i+4]);
    changeLabelValue(i+4,l_Conl,signalValue[i+4]);
    changeAllLineColorAfterTransimt(i+4);
}

void MainWindow::changeBoxLineColor(int i){

    if(box[i]->checkState()==Qt::Checked){
        f[i]->setStyleSheet(colorTrue1);
        f1[i]->setStyleSheet(colorTrue1);
        signalValue[i]=1;        
        changeLabelValue(i,lf,signalValue[i]);
        changeLabelValue(i,l_Conl,signalValue[i]);
        if((Error-1)==i) signalValue2[i]=0;
        else signalValue2[i]=1;
        changeAllLineColorAfterTransimt(i);
        changeInformLineColorAfterTransmit(i);
        changeMLineColorAfterInput();
        changeM2LineColorAfterInput();
    }
    else{
            f[i]->setStyleSheet(colorTrue0);
            f1[i]->setStyleSheet(colorTrue0);
            signalValue[i]=0;
            changeLabelValue(i,lf,signalValue[i]);
            changeLabelValue(i,l_Conl,signalValue[i]);
            if((Error-1)==i) signalValue2[i]=1;
            else signalValue2[i]=0;
            changeAllLineColorAfterTransimt(i);
            changeInformLineColorAfterTransmit(i);
            changeMLineColorAfterInput();
            changeM2LineColorAfterInput();      
    }
}

void MainWindow::changeBox1LineColor(int i){
    if(boxm1[i]->checkState()==Qt::Checked){
        switch ((int)i/4)
        {
            case 0:
            {
                if(signalValue[i%4]!=-1)
                {
                    if(signalValue[i%4]>0)
                    {
                        fm1[i%4]->setStyleSheet(colorTrue1);
                        fme1[i%4]->setStyleSheet(colorTrue1);
                    }
                    else
                    {
                        fm1[i%4]->setStyleSheet(colorTrue0);
                        fme1[i%4]->setStyleSheet(colorTrue0);
                    }
                    M1+=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm1[i%4]->setStyleSheet(colorDefault);
                    fme1[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 1:
            {
                if(signalValue[i%4]!=-1)
                {
                    if(signalValue[i%4]>0)
                    {
                        fm2[i%4]->setStyleSheet(colorTrue1);
                        fme2[i%4]->setStyleSheet(colorTrue1);
                    }
                    else
                    {
                        fm2[i%4]->setStyleSheet(colorTrue0);
                        fme2[i%4]->setStyleSheet(colorTrue0);
                    }
                    M2+=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm2[i%4]->setStyleSheet(colorDefault);
                    fme2[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 2:
            {
                if(signalValue[i%4]!=-1)
                {
                    if(signalValue[i%4]>0)
                    {
                        fm3[i%4]->setStyleSheet(colorTrue1);
                        fme3[i%4]->setStyleSheet(colorTrue1);
                    }
                    else
                    {
                        fm3[i%4]->setStyleSheet(colorTrue0);
                        fme3[i%4]->setStyleSheet(colorTrue0);
                    }
                    M3+=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm3[i%4]->setStyleSheet(colorDefault);
                    fme3[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
        }
    }
    else{
        switch ((int)i/4)
        {
            case 0:
            {
                if(signalValue[i%4]!=-1)
                {
                    fm1[i%4]->setStyleSheet(colorDefault);
                    fme1[i%4]->setStyleSheet(colorDefault);
                    M1-=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm1[i%4]->setStyleSheet(colorDefault);
                    fme1[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 1:
            {
                if(signalValue[i%4]!=-1)
                {
                    fm2[i%4]->setStyleSheet(colorDefault);
                    fme2[i%4]->setStyleSheet(colorDefault);
                    M2-=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm2[i%4]->setStyleSheet(colorDefault);
                    fme2[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 2:
            {
                if(signalValue[i%4]!=-1)
                {
                    fm3[i%4]->setStyleSheet(colorDefault);
                    fme3[i%4]->setStyleSheet(colorDefault);
                    M3-=signalValue[i%4];
                    changeMLineColor((int)i/4);
                    changeM2LineColorAfterInput();
                }
                else
                {
                    fm3[i%4]->setStyleSheet(colorDefault);
                    fme3[i%4]->setStyleSheet(colorDefault);
                }
                break;
            }
        }
    }
}
